<!--
@license
Copyright 2017 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../../../tf-imports/polymer.html">
<link rel="import" href="../../../iron-list/iron-list.html">
<link rel="import" href="../../../paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../../../paper-item/paper-item.html">
<link rel="import" href="../../../paper-listbox/paper-listbox.html">
<link rel="import" href="../../common/common_bundle.html">
<link rel="import" href="../facets-overview-row-stats/facets-overview-row-stats.html">
<link rel="import" href="../facets-overview-row-legend/facets-overview-row-legend.html">
<link rel="import" href="../facets-overview-chart/facets-overview-chart.html">

<dom-module id='facets-overview-table'>
  <template>
    <style>
      [hidden] {
        display: none !important;
      }
      .feature-name {
        font-weight: 500;
        max-width: 480px;
        word-wrap: break-word;
        margin: 0 0 4px 4px;
      }
      .table-name {
        font-weight: 500;
        word-wrap:break-word;
        padding: 4px 0 4px 8px;
        background: rgba(61,140,207,0.3);
        border: solid 0.5px rgba(0,0,0,0.1);
      }
      .feature-iron-list {
        overflow-x: hidden !important;
        height: 800px;
      }
      .chart-column {
        width: 280px;
        min-width: 280px;
        text-align: center;
      }
      .chart-column.header-cell {
        background: rgba(61,140,207,0.1);
        border-top: solid 0.5px rgba(0,0,0,0.1);
        border-bottom: solid 0.5px rgba(0,0,0,0.1);
        border-right: solid 0.5px rgba(0,0,0,0.1);
        padding-left: 8px;
      }
      .numeric-row {
        min-height: 80px;
        display: table-row;
      }
      .categorical-row {
        min-height: 98px;
        display: table-row;
      }
      .table-cell {
        display: table-cell;
        vertical-align: top;
        font-size: 14px;
        line-height: 1.2em;
      }
      .chart-column.table-cell {
        display: table-cell;
        vertical-align: top;
        padding: 10px 0 0 20px
        margin-right: 10px;
      }
      .header-cell {
        position: relative;
        min-height: 60px;
      }
      paper-checkbox {
        --paper-checkbox-size: 12px;
        --paper-checkbox-label-spacing: 2px;
      }
      .control-holder {
        position: absolute;
        bottom: 0;
        width: 100%;
      }
      .checkbox-holder {
        display: flex;
        padding-bottom: 2px;
      }
      .chart-checkbox {
        padding-right: 8px;
        font-size: 12px;
      }
      #weightbox {
        font-style: italic;
      }
      paper-dropdown-menu {
        display: block;
        width: 150px;
        --paper-input-container-label --paper-input-container-input: {
          font-size: 14px;
        }
      }
      .hidden {
        display: none;
      }
    </style>
    <div class$="[[_getTableWrapperClass(features)]]">
      <div class="header-row">
        <div class="header-cell table-cell">
          <div class="table-name">[[_getTitle(numeric)]] Features ([[_getFeatureCountText(dataModel, numeric, features)]])
          </div>
          <facets-overview-row-legend numeric="[[numeric]]"
                                show-weighted="[[_showWeighted]]"
                                has-custom="[[_hasCustomStats(dataModel)]]"
                                data-model="[[dataModel]]">
          </facets-overview-row-legend>
        </div>
        <div class="table-cell chart-column header-cell">
          <div class="control-holder">
            <paper-dropdown-menu label="Chart to show">
              <paper-listbox class="dropdown-content" selected="{{_chartSelection}}" attr-for-selected="value" slot="dropdown-content">
                <template is="dom-repeat" items="[[_chartSelectionTypes]]">
                  <paper-item value="[[item]]">[[item]]</paper-item>
                </template>
              </paper-listbox>
            </paper-dropdown-menu>
            <div class="checkbox-holder">
              <paper-checkbox class="chart-checkbox" id="logbox" checked="{{_logScale}}">log</paper-checkbox>
              <paper-checkbox class="chart-checkbox"  id="expandbox" checked="{{_expandCharts}}">expand</paper-checkbox>
              <template is="dom-if" if="[[_hasWeightedHistogram(features, datasetCheckboxes)]]">
                <paper-checkbox class="chart-checkbox"  id="weightbox" checked="{{_showWeighted}}">weighted</paper-checkbox>
              </template>
              <template is="dom-if" if="[[_hasMultipleDatasets(dataModel)]]">
                <paper-checkbox class="chart-checkbox"  id="percentbox" checked="{{_showPercentage}}" disabled="[[_chartSelectionHasQuantiles(_chartSelection)]]">percentages</paper-checkbox>
              </template>
            </div>
          </div>
        </div>
      </div>
      <iron-list items="[[features]]"
                 as="feature"
                 class="feature-iron-list">
        <template>
          <div class$="[[_getTableRowClass(numeric)]]">
            <div class="table-cell">
              <div class="feature-name">[[_getFeatureName(feature)]]</div>
              <template is="dom-repeat" items="[[_getDatasets(dataModel)]]" as="dataset" index-as="datasetIndex">
                <template is="dom-if" if="[[_shouldShowDataset(datasetIndex, datasetCheckboxes)]]">
                  <div>
                    <facets-overview-row-stats
                                        stats="[[_getStats(dataModel, dataset, feature)]]"
                                        custom-stats="[[_getAllCustomStats(dataModel, feature)]]"
                                        show-weighted="[[_showWeighted]]"
                                        has-custom="[[_hasCustomStats(dataModel)]]"
                                        data-model="[[dataModel]]"
                                        dataset-index="[[datasetIndex]]"
                                        compare-mode="[[compareMode]]">
                    </facets-overview-row-stats>
                  </div>
                </template>
              </template>
            </div>
            <div class$="[[_getChartClass(_expandCharts)]]">
              <facets-overview-chart data="[[_getChartData(dataModel, feature, datasetCheckboxes)]]"
                               data-model="[[dataModel]]"
                               feature="[[_getFeatureName(feature)]]"
                               selection="{{featureSliceSelection}}"
                               log-scale="[[_logScale]]"
                               show-weighted="[[_showWeighted]]"
                               show-percentage="[[_showPercentage]]"
                               chart-selection="[[_chartSelection]]"
                               expand-chart="[[_expandCharts]]">
              </facets-overview-chart>
            </div>
          </div>
        </template>
      </iron-list>
    </div>
  </template>
</dom-module>
<script src="facets-overview-table.js"></script>
